package Worker;

import Module.StatusService;
import Module.WorkService;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Hashtable;

/**
 * Created by IntelliJ IDEA.
 * User: lihengfeng
 * Date: 11-5-19
 * Time: 
 * To change this template use File | Settings | File Templates.
 */
public class StatusInfo extends UnicastRemoteObject
            implements StatusService {
    RegistryInfo registryInfo;


    private StatusService.WorkerStatus workerStatus;
    private Hashtable<Integer, JobStatus> jobsStatus;

    public StatusInfo(RegistryInfo registryInfo) throws RemoteException {
        super();
        this.registryInfo = registryInfo;
        this.jobsStatus = new Hashtable<Integer, StatusService.JobStatus>();
    }

    public void setWorkerStatus(WorkerStatus status){
        this.workerStatus = status;
    }

    public WorkerStatus getWorkerStatus(String authCode) throws RemoteException {
        if(!registryInfo.verifyAuthCode(authCode)) {
            return null;
        }

        return this.workerStatus;
    }

    public void setJobStatus(int jobID, JobStatus jobStatus, String authCode) throws RemoteException {
        if(!registryInfo.verifyAuthCode(authCode)) {
            return;
        }
        
        this.jobsStatus.put(jobID,jobStatus);
    }
    
    public void setJobStatus(int jobID, JobStatus jobStatus){       
        this.jobsStatus.put(jobID,jobStatus);
    }

    public JobStatus getJobStatus(int jobID, String authCode ) throws RemoteException{
        if(!registryInfo.verifyAuthCode(authCode)) {
            return null;
        }

        return this.jobsStatus.get(jobID);
    }

	public Hashtable<Integer, JobStatus> getJobsStatus(String authCode)
			throws RemoteException {
		 if(!registryInfo.verifyAuthCode(authCode)) {
	            return null;
	       }
		 return this.jobsStatus;

	}

	public void clearStatus(String authCode) throws RemoteException {
		this.jobsStatus.clear();
	}
}
